package RMI.client.PromoManager.GUI;

/*
 * ConsultarVendas.java
 *
 * Created on 2 de Fevereiro de 2008, 00:47
 */



import RMI.server.interfaces.Comunication;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.sql.Date;
import sisvenda.vendas.VendasBean;

/**
 *
 * @author  Cl�udio
 */
public class ConsultarVendas extends javax.swing.JFrame {

	private Comunication objRemoto=null;
    
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	DefaultTableModel tmVendas = new DefaultTableModel(null, new String[] {"Codigo","Cliente","Produto", "Quantidade", "Data"});
    List<VendasBean> vendas;
    
    /** Creates new form ConsultarVendas */
    public ConsultarVendas() {
		super();
		initComponents();
		int port = 1099;
        try {
            Registry registry = LocateRegistry.getRegistry("localhost", port);
            objRemoto = (Comunication) registry.lookup("clienteCalculadora");
            
        } catch (NotBoundException e) {
            System.out.println("Aplica��o n�o encontrada: " + e.getMessage());
        } catch (RemoteException e) {
            System.out.println(e);
        }
	}
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        ftfDataInicio = new JFormattedTextField(DateFormat.getDateInstance());
        ftfDataFim = new JFormattedTextField(DateFormat.getDateInstance());
        jLabel2 = new javax.swing.JLabel();
        btPesquisar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbVendas = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Consultar Vendas");
        jLabel1.setText("Data Inicio:");

        jLabel2.setText("Data Fim:");

        btPesquisar.setText("Pesquisar");
        btPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
					btPesquisarActionPerformed(evt);
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        });

        tbVendas.setModel(tmVendas);
        jScrollPane1.setViewportView(tbVendas);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(9, 9, 9)
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(ftfDataInicio, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ftfDataFim, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btPesquisar)))
                .addContainerGap(15, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btPesquisar)
                    .addComponent(ftfDataFim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ftfDataInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(22, 22, 22))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void btPesquisarActionPerformed(java.awt.event.ActionEvent evt) throws RemoteException {//GEN-FIRST:event_btPesquisarActionPerformed
        pesquisarVendas();
    }//GEN-LAST:event_btPesquisarActionPerformed
    
    private void pesquisarVendas() throws RemoteException{
               if (ftfDataInicio.getValue() == null){
            JOptionPane.showMessageDialog(this, "Informe a data Inicial!");
            ftfDataInicio.requestFocus();
        } else if (ftfDataFim.getValue() == null){
            JOptionPane.showMessageDialog(this, "Informe a data Final!");
            ftfDataFim.requestFocus();
        } else {
            while (tmVendas.getRowCount() > 0){
                tmVendas.removeRow(0);
            }
            SimpleDateFormat formato = new SimpleDateFormat("yyyy-MM-dd");
            java.util.Date dataInicio = (java.util.Date) ftfDataInicio.getValue();
            System.out.println(formato.format(dataInicio));
            java.util.Date dataFim = (java.util.Date) ftfDataFim.getValue();
            vendas = objRemoto.listarVendas(Date.valueOf(formato.format(dataInicio)), Date.valueOf(formato.format(dataFim)));
            String [] campos = new String[] {null, null, null, null, null};
            for (int i = 0; i < vendas.size(); i++){
                tmVendas.addRow(campos);
                tmVendas.setValueAt(vendas.get(i).getCodigo(), i, 0);
                tmVendas.setValueAt(vendas.get(i).getCodigoCliente(), i, 1);
                tmVendas.setValueAt(vendas.get(i).getCodigoProduto(), i, 2);
                tmVendas.setValueAt(vendas.get(i).getQuantidade(), i, 3);
                tmVendas.setValueAt(vendas.get(i).getDataVenda(), i, 4);
            }
            if (vendas.size() == 0){
                JOptionPane.showMessageDialog(this, "Nenhum registro encontrado!");
            }
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ConsultarVendas().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btPesquisar;
    private javax.swing.JFormattedTextField ftfDataFim;
    private javax.swing.JFormattedTextField ftfDataInicio;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbVendas;
    // End of variables declaration//GEN-END:variables
    
}

